<?php
/*
 * This file is part of the BooXtore Student project.
 *
 * (c) Francis Backeland f.backeland@gmail.com
 * (c) Laurine Chaplot chaplot.laurine@orange.fr
 * (c) Julien Dutot julien.dutot@gmail.com
 * (c) Romain Guillaume romain-guillaume@orange.fr
 * (c) Guillaume Paringaux g.paringaux@gmail.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code
 */
namespace BXTR\LayoutBundle\Form\Type;

use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

/**
 * Back button, special button, with 'validation_groups' disabled, so the form will always be valid
 * when validated with this button.
 *
 * @uses IconButtonType
 */
class BackButtonType extends AbstractType
{
    /** 
     * Form clean name : back_button
     */
    public function getName()
    {
        return 'back_button';
    }

    /**
     * Sames options as normal buttons, icon, label, attr
     * A new url option which is the target page for the button.
     *
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'icon' => 'arrow-left',
            'label' => 'Form.Back',
            'attr' => Array('class' => 'btn-default'),
            'mapped' => false,
        ));

        $resolver->setRequired(array(
            'url'
        ));
    }

    /**
     * Pass the target url and the icon to the view.
     *
     * @param FormView $view
     * @param FormInterface $form
     * @param Array $options
     */
    public function buildView(FormView $view, FormInterface $form, Array $options)
    {
        $view->vars['target_url'] = $options['url'];
        $view->vars['icon_class'] = $options['icon'];
    }

}
